package com.quanyan.club.web;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.db.TbOrderRefund;
import com.quanyan.club.pullclub.service.PullClubService;
import com.quanyan.club.service.ClubActivityService;
import com.quanyan.club.service.ClubRefundService;
import com.quanyan.club.service.ClubWidthdrawService;
import com.quanyan.orderpay.response.mq.OrderNotifyMsg;
import com.quanyan.orderpay.response.mq.RefundNotifyMsg;
import com.quanyan.orderpay.response.mq.WithdrawTransferNotify;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zhuo.shi on 2016/8/27.
 *
 * 订单支付成功、退款成功、订单超时支付关闭回调处理入口
 */
@RequestMapping(value = "/api/club/callBack")
@RestController
public class OrderPayCallbackController {

    private Logger logger = LoggerFactory.getLogger(OrderPayCallbackController.class);

    @Autowired
    private ClubActivityService clubActivityService;

    @Autowired
    private ClubRefundService clubRefundService;

    @Autowired
    private ClubWidthdrawService clubWidthdrawService;

    @Autowired
    private PullClubService pullClubService;

    @RequestMapping(value = "/orderPaySuccess")
    public APIResponse activityOrderPaySuccess(@RequestBody OrderNotifyMsg orderNotifyMsg) {
        logger.warn("orderPaySuccess 回调返回：" + JSON.toJSONString(orderNotifyMsg));
        return clubActivityService.
                handleActivityPayResponse(orderNotifyMsg.getOrderNo(), orderNotifyMsg);
    }

    @RequestMapping(value = "/orderRefundNotify")
    public APIResponse orderRefundNotify(@RequestBody RefundNotifyMsg refundNotifyMsg) {

        logger.warn("orderRefundNotify 回调返回：" + JSON.toJSONString(refundNotifyMsg));
        String refundOrderNo = refundNotifyMsg.getOutTradeRefundOrderNo(); //退款流水号

        TbOrderRefund tbOrderRefund = clubRefundService.queryOneTbOrderRefundRecord(refundNotifyMsg.getUid(), refundOrderNo);
        logger.warn("查询退款记录：tbOrderRefund=" + JSON.toJSONString(tbOrderRefund));
        if (tbOrderRefund == null) {
            return APIResponse.returnFail("退款订单不存在");
        }

        String businessOrderNo = refundNotifyMsg.getOutTradeNo(); //订单号

        boolean success = false;
        switch (tbOrderRefund.getRefundType().intValue()) {
            //1: 活动结算AA退款 2:用户取消已支付订单退款 3:管理员取消已支付订单退款() 4:活动未达到开启条件,给已经报名支付成功的人退款 5现场管理人补请假退款 6百灵鸟官方后台管理员退款
            case 1:
                APIResponse apiResponse = clubActivityService.
                        handleActivitySettlementAARefundCallback(refundNotifyMsg, tbOrderRefund);
                if (!apiResponse.isRet()) {
                    success = false;
                } else success = true;
                break;

            case 2:
                success =
                        clubActivityService.cancelOrderNotifyCallback(businessOrderNo);
                if (!success) {
                    logger.error("处理俱乐部订单取消退款失败，refundOrderNo", refundOrderNo);
                }
                break;

            case 3:
                success = clubActivityService.
                        cancelActivityOrderAdminCallback(refundNotifyMsg);
                if (!success) {
                    logger.error("处理俱乐部活动订单取消退款失败，refundOrderNo=", refundOrderNo);
                }
                break;

            case 4:
                success =
                        clubActivityService.cancelOrderNotifyCallback(businessOrderNo);
                if (!success) {
                    logger.error("处理  活动未达到开启条件(报名人数未满), 给已经报名支付成功的人退款失败，MQ messageId={}", refundOrderNo);
                }
                break;

            case 5:
                APIResponse supplementLeaveRes = clubActivityService
                        .supplementLeaveRefundOrderCallBack(refundNotifyMsg);
                if (!supplementLeaveRes.isRet()) {
                    success = false;
                } else success = true;
                break;
            case 6:
                success =
                        clubActivityService.cancelOrderNotifyCallbackAdmin(businessOrderNo);
                if (!success) {
                    logger.error("处理俱乐部订单取消退款失败，refundOrderNo", refundOrderNo);
                }
                break;
            default:
                break;
        }
        return success ? APIResponse.returnSuccess() : APIResponse.returnFail("退款处理失败");
    }

    @RequestMapping(value = "/orderExceptionRefundNotify")
    public APIResponse orderExceptionRefundNotify() {

        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/orderCloseNotify")
    public APIResponse orderCloseNotify(@RequestBody OrderNotifyMsg orderNotifyMsg) {
        logger.warn("orderCloseNotify 回调返回：" + JSON.toJSONString(orderNotifyMsg));
        return clubActivityService.
                handleActivityOrderCloseCallBack(orderNotifyMsg);
    }

    @RequestMapping(value = "/withdrawNotify")
    public APIResponse withdrawNotify(@RequestBody WithdrawTransferNotify notify) {
        logger.warn("withdrawNotify 回调返回：" + JSON.toJSONString(notify));
        return clubWidthdrawService.withdrawCallbackNotify(notify);
    }

    @RequestMapping(value = "/pull/withdrawNotify")
    public APIResponse pullWithdrawNotify(@RequestBody WithdrawTransferNotify notify) {
        logger.warn("pullWithdrawNotify 回调返回：" + JSON.toJSONString(notify));
        return pullClubService.withdrawCallbackNotify(notify);
    }
}
